package SentenceTranslator;

import java.util.ArrayList;
import java.util.List;
import Include.TACLine;
import Common.TacOperandType;
import Include.ASMConstructor;
import Common.REG;
import Common.POSTYPE;
import Main.SymbolManager;


public class FunretTranslator {
    public List<String> SentenceTranslate(SymbolManager symbolManager, TACLine tacLine) {
        List<String> asmLines = new ArrayList<>();
        String retSrc = tacLine.src1.value;
        int retMem = symbolManager.ret();
        if(tacLine.src1.operType==TacOperandType.IMM){
            asmLines.add(ASMConstructor.construct_asm("mov", String.valueOf(retMem), retSrc));
        } else if(tacLine.src1.operType == TacOperandType.VAR) {
            String retSrcEncode = symbolManager.encodeVar(retSrc);
            POSTYPE pos = symbolManager.position(retSrcEncode);
            if (pos == POSTYPE.REG) {
                REG retSrcReg = symbolManager.avalueReg(retSrcEncode);
                asmLines.add(ASMConstructor.construct_asm("mov", String.valueOf(retMem), retSrcReg));
            } else if (pos == POSTYPE.MEM) {
                int retSrcMem = symbolManager.avalueMem(retSrcEncode);
                asmLines.add(ASMConstructor.construct_asm("mov", REG.EDI, String.valueOf(retSrcMem)));
                asmLines.add(ASMConstructor.construct_asm("mov", String.valueOf(retMem), REG.EDI));
            } else if (pos == POSTYPE.GLOBAL) {
                asmLines.add(ASMConstructor.construct_asm("mov", String.valueOf(retMem), retSrc));
            } else {
//                Logger.getLogger(FunretTranslator.class.getName()).log(Level.SEVERE, "funret default error");
            }
        }
        return asmLines;
    }
}
